﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            LoadDirectories();
        }

        private void LoadDirectories()
        {
            DirectoryInfo dirInfo = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
            foreach (DirectoryInfo dir in dirInfo.GetDirectories())
            {
                TreeViewItem item = new TreeViewItem
                {
                    Header = dir.Name,
                    Tag = dir
                };
                item.Items.Add(null); 
                item.Expanded += Folder_Expanded;
                FoldersTree.Items.Add(item);
            }
        }

        private void Folder_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = (TreeViewItem)sender;
            if (item.Items.Count == 1 && item.Items[0] == null)
            {
                item.Items.Clear();
                DirectoryInfo dir = (DirectoryInfo)item.Tag;
                try
                {
                    foreach (DirectoryInfo subDir in dir.GetDirectories())
                    {
                        TreeViewItem subItem = new TreeViewItem
                        {
                            Header = subDir.Name,
                            Tag = subDir
                        };
                        subItem.Items.Add(null);
                        subItem.Expanded += Folder_Expanded;
                        item.Items.Add(subItem);
                    }
                    foreach (FileInfo file in dir.GetFiles())
                    {
                        item.Items.Add(new TreeViewItem { Header = file.Name, Tag = file });
                    }
                }
                catch { }
            }
        }

        private void FoldersTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeViewItem item = (TreeViewItem)FoldersTree.SelectedItem;
            FilesList.Items.Clear();
            if (item != null && item.Tag is DirectoryInfo)
            {
                DirectoryInfo dir = (DirectoryInfo)item.Tag;
                try
                {
                    foreach (DirectoryInfo subDir in dir.GetDirectories())
                    {
                        ListViewItem lvItem = new ListViewItem { Content = subDir.Name + " [Folder]", Tag = subDir.FullName };
                        FilesList.Items.Add(lvItem);
                    }
                    foreach (FileInfo file in dir.GetFiles())
                    {
                        ListViewItem lvItem = new ListViewItem { Content = file.Name, Tag = file.FullName };
                        FilesList.Items.Add(lvItem);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("无法加载文件: " + ex.Message);
                }
            }
        }

        private void FilesList_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (FilesList.SelectedItem != null)
            {
                ListViewItem item = FilesList.SelectedItem as ListViewItem;
                if (item != null && item.Tag != null && item.Content.ToString().EndsWith(".txt"))
                {
                    string filePath = item.Tag.ToString(); 
                    if (File.Exists(filePath))
                    {
                        try
                        {
                            string content = File.ReadAllText(filePath);
                            MessageBox.Show(content, "文本文件内容", MessageBoxButton.OK, MessageBoxImage.Information);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("无法读取文件: " + ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                    }
                    else
                    {
                        MessageBox.Show("文件不存在: " + filePath, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
            }
        }



        private void Exit_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void Refresh_Click(object sender, RoutedEventArgs e)
        {
            FoldersTree.Items.Clear();
            LoadDirectories();
        }
    }
}
